package nl.jworks.epub.organizer.dao.hibernate;

import nl.jworks.epub.organizer.dao.BookDao;
import nl.jworks.epub.organizer.domain.Book;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * http://www.java-forums.org/blogs/spring-framework/508-using-spring-data-access-support-hibernate.html
 */
@Repository
public class HibernateBookDao implements BookDao {
    private SessionFactory sessionFactory;

    @Autowired
    public HibernateBookDao(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    private Session currentSession() {
        return sessionFactory.getCurrentSession();
    }

    @Override
    public void saveBook(Book book) {
        currentSession().save(book);
    }

    @Override
    public Book getBookById(long id) {
        return (Book)currentSession().get(Book.class, id);
    }

    @Override
    public void updateBook(Book book) {
        currentSession().update(book);
    }
}
